package servlet;

import bean.Fruit;
import dao.impI.FruitDaoImpI;
import service.ImpI.FruitServiceImpI;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/productListServlet",loadOnStartup = 1,
    initParams = {
        @WebInitParam(name="encoding",value="utf-8")
    }
)
public class ProductListServlet extends HttpServlet {

    String encoding=null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        //FruitDaoImpI fruitDaoImpI = new FruitDaoImpI();
        //List<Fruit> fruitList = fruitDaoImpI.findAllFruit();
        FruitServiceImpI fruitServiceImpI = new FruitServiceImpI();
        List<Fruit> fruitList = fruitServiceImpI.findAllFruit();
        System.out.println(fruitList);
        req.setAttribute("fruitList",fruitList);
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }


}
